package org.starsource.forum.server.dao.test;

import static org.junit.Assert.*;

import java.sql.Timestamp;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.server.domain.LoginLog;
import org.starsource.forum.server.domain.User;
import org.starsource.forum.server.dao.LoginLogDao;
import org.starsource.forum.server.dao.UserDao;

public abstract class AbsTestLoginLogDao {
//	private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
//	.getLogger(AbsTestLoginLogDao.class.getName());

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private LoginLogDao loginLogDao;
	
	@Test
	public void testSave() throws DatabaseException {
		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		LoginLog loginLog1 = new LoginLog(user1, "192.168.8.1", new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginLogDao.save(loginLog1);
		
		LoginLog loginLog2 = loginLogDao.getLoginLog(loginLog1.getId());
		assertEquals(loginLog1, loginLog2);

		LoginLog loginLog3 = new LoginLog(null, null, null);
		loginLogDao.save(loginLog3);
		
		LoginLog loginLog4 = loginLogDao.getLoginLog(loginLog3.getId());
		assertEquals(loginLog3, loginLog4);
	}

//	@Test
//	public void testUpdate() throws DatabaseException {
//		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
//		userDao.save(user1);
//		
//		User user2 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
//		userDao.save(user2);
//		
//		LoginLog loginLog1 = new LoginLog(user1, "192.168.8.1", new Timestamp(new java.util.Date().getTime()/1000*1000));
//		loginLogDao.save(loginLog1);
//		
//		int loginLogId = loginLog1.getId();
//		
//		try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e) {
//			fail("Test fail because thread sleep");
//		}
//		
//		loginLog1.setUser(user2);
//		loginLog1.setIp("10.17.42.1");
//		loginLog1.setTimestamp(new Timestamp(new java.util.Date().getTime()/1000*1000));
//		loginLogDao.update(loginLog1);
//		
//		LoginLog loginLog2 = loginLogDao.getLoginLog(loginLogId);
//		assertEquals(loginLog1, loginLog2);
//
//		loginLog1.setUser(null);
//		loginLog1.setIp(null);
//		loginLog1.setTimestamp(null);
//		loginLogDao.update(loginLog1);
//		
//		LoginLog loginLog3 = loginLogDao.getLoginLog(loginLogId);
//		assertEquals(loginLog1, loginLog3);
//	}
//
//	@Test
//	public void testDelete() throws DatabaseException {
//		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
//		userDao.save(user1);
//		
//		LoginLog loginLog1 = new LoginLog(user1, "192.168.8.1", new Timestamp(new java.util.Date().getTime()/1000*1000));
//		loginLogDao.save(loginLog1);
//
//		int loginLogId = loginLog1.getId();
//		loginLogDao.delete(loginLog1);
//		
//		try {
//			loginLogDao.getLoginLog(loginLogId);
//			assertTrue(false);
//		} catch (DataNotFoundException e) {
//		}
//	}
//
//	@Test
//	public void testGetLoginLog() throws DatabaseException {
//		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
//		userDao.save(user1);
//		
//		LoginLog loginLog1 = new LoginLog(user1, "192.168.8.1", new Timestamp(new java.util.Date().getTime()/1000*1000));
//		loginLogDao.save(loginLog1);
//		
//		LoginLog loginLog2 = loginLogDao.getLoginLog(loginLog1.getId());
//		assertEquals(loginLog1, loginLog2);
//	}
//	
//	@Test
//	public void testGetLastLoginLog() throws DatabaseException {
//		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "NAME_FOR_TEST", "DIRECTORY_FOR_TEST");
//		userDao.save(user1);
//		
//		int round = 50;
//		LoginLog loginLog1 = null;
//		
//		for(int i = 0; i < round; i++) {
//			loginLog1 = new LoginLog(user1, "192.168.8." + i, new Timestamp(new java.util.Date().getTime()/1000*1000));
//			loginLogDao.save(loginLog1);
//			try {
//				Thread.sleep(50);
//			} catch (InterruptedException e) {
//				fail("Test fail because thread sleep");
//			}
//		}
//		
//		LoginLog loginLog2 = loginLogDao.getLastLoginLog(user1);
//		assertEquals(loginLog1, loginLog2);
//	}
}
